package com.wc.算法提高课.A第一章_动态规划.背包模型.机器分配;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/9 21:19
 * @description https://www.acwing.com/problem/content/1015/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 16;
    static int[][] a = new int[N][N];
    // 表示前i家分了j台的最大价值
    static int[][] f = new int[N][N];
    // 最大的时候每家分了几台
    static int[][][] nums = new int[N][N][N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int maxV = 0, num = 0;
                // 第i家分了k台，一共是j台
                for (int k = 0; k <= j; k++) {
                    if (maxV < f[i - 1][j - k] + a[i][k]) {
                        maxV = f[i - 1][j - k] + a[i][k];
                        num = k;
                    }
                }
                f[i][j] = maxV;
                if (n >= 0) System.arraycopy(nums[i - 1][j - num], 1, nums[i][j], 1, n);
                nums[i][j][i] = num;
            }
        }
        out.println(f[n][m]);
        for (int i = 1; i <= n; i++) {
            out.println(i + " " + nums[n][m][i]);
        }
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
